package com.zjbbkj.baibai.netty.message.sendpackage;

import com.zjbbkj.baibai.common.utils.wanli.SessionIdGeneratorUtils;
import lombok.extern.slf4j.Slf4j;
import static com.zjbbkj.baibai.netty.constants.NettySomeThing.*;

@Slf4j
public class RUN {

    /**
     * 充电指令
     *
     * @param STATUS
     * @param PORT
     * @param TIME
     * @param PRAY
     * @return
     */
    public static String RUN_DEVICE(String STATUS, String PORT, String TIME, String PRAY) {
        try {
            //_052RUN147852/1:777777:[0,1,2,3]:1200:你好测试功能\r\n
            //端口号:状态:时长:祈福语
            String type = "RUN";
            //随机数生成
            String random = SessionIdGeneratorUtils.getRandomStrID();
            String separator = "/";  //分隔符
            String portStr = "[" + PORT + "]";
            // + powerLength + power
            //new String(PRAY.getBytes("UTF-8"),"GB2312")
            String orderId = SessionIdGeneratorUtils.getRandomStrID();
            log.info("------------------------------新增订单号为：" + orderId);
            String cmd = START + NUM + type + random + separator + STATUS + ":" + orderId + ":" + portStr + ":" +
                    TIME + ":" + PRAY + "\r\n";
            String length = String.valueOf(cmd.length() + PRAY.length());
            if (length.length() == 2) {
                length = "0" + length;
            }
            cmd = START + length + type + random + separator + STATUS + ":" + orderId + ":" + portStr + ":" +
                    TIME + ":" + PRAY + "\r\n";
            if (ON_OFF.equals("1")) {
                log.info(cmd);
            }
            return cmd;
        } catch (Exception e) {
            log.info(e.getMessage());
            return "null";
        }
    }

    /**
     * 充电指令(老版本)
     *
     * @param PORT
     * @param TIME
     * @param PRAY
     * @return
     */
    public static String RUN_DEVICE_OLD(String PORT, String TIME, String PRAY) {
        try {
            //端口号:状态:时长:祈福语
            String type = "RUN";
            //随机数生成
            String random = SessionIdGeneratorUtils.getRandomStrID();
            String separator = "/";  //分隔符
            String portStr = "[" + PORT + "]";
            // + powerLength + power
            //new String(PRAY.getBytes("UTF-8"),"GB2312")
            String orderId = SessionIdGeneratorUtils.getRandomStrID();
            log.info("------------------------------新增订单号为：" + orderId);
            String cmd = START + NUM + type + random + separator + portStr + ":1:" +
                    TIME + ":" + PRAY + "\r\n";
            Integer length = cmd.length() + PRAY.length();
            cmd = START + "0" + length + type + random + separator + portStr + ":1:" +
                    TIME + ":" + PRAY + "\r\n";
            if (ON_OFF.equals("1")) {
                log.info(cmd);
            }
            return cmd;
        } catch (Exception e) {
            log.info(e.getMessage());
            return "";
        }
    }

    /**
     * 关闭订单/修改订单
     *
     * @param STATUS
     * @param TIME
     * @param ORDERID
     * @param PORT
     * @param PRAY
     * @return
     */
    public static String RUN_DEVICE_DELETE_OR_MODIFY(String STATUS, String TIME, String ORDERID, String PORT, String PRAY) {
        try {
            String type = "RUN";
            String random = SessionIdGeneratorUtils.getRandomStrID();
            String separator = "/";  //分隔符
            String portStr = "[" + PORT + "]";
            String cmd = START + NUM + type + random + separator;
            if (STATUS.equals("3")) {
                //关闭订单
                cmd = cmd + STATUS + ":" + ORDERID + "\r\n";
                String length = String.valueOf(cmd.length());
                if (length.length() == 2) {
                    length = "0" + length;
                }
                cmd = START + length + type + random + separator + STATUS + ":" + ORDERID + "\r\n";
            } else if (STATUS.equals("2")) {
                //修改订单
                cmd = cmd + STATUS + ":" + ORDERID + ":" + portStr + ":" + TIME + ":" + PRAY + "\r\n";
                String length = String.valueOf(cmd.length() + PRAY.length());
                if (length.length() == 2) {
                    length = "0" + length;
                }
                cmd = START + length + type + random + separator + STATUS + ":" + ORDERID + ":" + portStr + ":" + TIME + ":" +
                        PRAY + "\r\n";
            }
            if (ON_OFF.equals("1")) {
                log.info(cmd);
            }
            return cmd;
        } catch (Exception e) {
            log.info(e.getMessage());
            return "";
        }
    }
}
